﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zoo.Cat.Application.Jobs;
using Zoo.Cat.Domain.Entities.Jobs;
using Zoo.Cat.Domain.Unity;
using Zoo.Cat.Application.Lovs;
using Zoo.Cat.Domain.Entities.Lovs;

namespace Zoo.Cat.Web.Controllers
{
    public class JobController : Controller
    {
        #region Fields
        private readonly IJobService _jobService;
        private readonly ILovService _lovService;
        #endregion

        #region Ctor
        public JobController(IJobService jobService,ILovService lovService)
        {
            _jobService = jobService;
            _lovService = lovService;
        }
        #endregion

        //
        // GET: /Job/
        public ActionResult Index(String jobName)
        {
            IEnumerable<Job> list = null;
            if (!String.IsNullOrWhiteSpace(jobName)) 
            {
                list = _jobService.FindJobList(jobName).ToList();
                var categoryIdList = _jobService.GetAssociatedLovIdList(list.AsQueryable(), LovType.Category);
                var categoryList = _lovService.GetLovInfoList(categoryIdList).ToList();
                var groupCategoryIdList = categoryList.Select(item => item.ParentID.Value).Distinct();
                var groupCategoryList = _lovService.GetLovInfoList(groupCategoryIdList).ToList();
                ViewBag.CategoryList = categoryList;
                ViewBag.GroupCategoryList = groupCategoryList;
            }
            //ViewBag
            if (!list.Any()) 
            {
                //return 
            }
            return View(list.ToList());
        }

        private void SetCategoryMenu(IEnumerable<Lov> categoryList)
        {
            
        }

        //
        // GET: /Job/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Job/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Job/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Job/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Job/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Job/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Job/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
